package com.yugao.fintech.vctgone.log.util;

import jakarta.servlet.http.HttpServletRequest;
import com.yugao.fintech.vctgone.auth.api.dto.LogDTO;
import com.yugao.fintech.vctgone.util.ServletUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Optional;

public class LogUtils {
    public static LogDTO getDefaultLogDTO() {
        LogDTO logDTO = new LogDTO();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Optional.ofNullable(requestAttributes).ifPresent(attributes -> {
            HttpServletRequest request = attributes.getRequest();
            logDTO.setIp(ServletUtil.getClientIP(request));
            logDTO.setRequestMethod(request.getMethod());
        });
        return logDTO;
    }

    public static String getToken() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            return request.getHeader(HttpHeaders.AUTHORIZATION);
        }
        return null;
    }
}
